outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(expand = c(.1, .1),
labels = label_number(scale_cut = cut_short_scale()),
limits = c(0,NA))
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(expand = c(0, .1),
labels = label_number(scale_cut = cut_short_scale()),
limits = c(0,NA))
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
limits = c(0,NA))
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
limits = c(0,NULL))
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
limits = c(0,NA))
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(limits = c(0,NA),
expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
)
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
C_filterd_data<-C_filterd_data%>%filter(Prosessing_Step=="ICP")
c_vector<-brewer.pal(length(elements_conc), "Set3")
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(limits = c(0,max(filter(datum,element%in%e, Prosessing_Step=="ICP")$mean,)),
expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
)
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(limits = c(0,max(filter(datum,element%in%e, Prosessing_Step=="ICP")$mean)),
expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
)
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(limits = c(0,max(filter(datum,element%in%e, Prosessing_Step=="ICP")$mean)*1.1),
expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
)
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
violin_plot<-function(
datum = C_filterd_data ,
e,
ylabel = NULL,
pick_colors = c_vector
){
#create color pallet
color_vector<-
data.frame(element=elements_conc,
c=pick_colors)%>%
filter(element%in%e)%>%
select(c)%>%
pull()
#make plot
datum%>%
filter(element%in%e, Prosessing_Step=="ICP")%>%
ggplot(aes(x=element,y=mean,fill=element))+
geom_violin(width=1.4)+
geom_boxplot(width=0.1,
color="white",
alpha=1,
outlier.shape = "cross")+
geom_point(aes(color=matrix),position = position_dodge(width = 0.5))+
coord_flip()+
xlab("")+
ylab(ylabel)+
scale_fill_manual(values =color_vector)+
my_theme+
theme(legend.position="none")+
scale_y_continuous(limits = c(0,max(filter(datum,element%in%e, Prosessing_Step=="ICP")$mean)*1.01),
expand = c(0, 1),
labels = label_number(scale_cut = cut_short_scale()),
)
}
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
ggsave("Figuere_B_with_matrix_dots.png", device = png, width = 160, height = 207, units = "mm")#A4 margens and space for caption
legend<-get_legend(violin_plot(e = c("Fe","Ca"))+
theme(legend.position = "right"))
legend
plot_grid(legend)
ggsave("Figuere_B_Legend.png", device = png, width = 160, height = 207, units = "mm")#A4 margens and space for caption
ggsave("Figuere_B_Final.png", device = png, width = 160, height = 207, units = "mm")#A4 margens and space for caption
violin_plot(e = c("Fe","Ca"))+
violin_plot(e = c("Mn","K","Zn"))+
violin_plot(e = c("P","Ni","Ti","Cr"))+
violin_plot(e = c("Sr","Pb","As"),ylabel ="Conc (mg/kg)")+
plot_layout(ncol = 1,heights =c(2,3,4,3))
ggsave("Figuere_B_Final.png", device = png, width = 160, height = 207, units = "mm")#A4 margens and space for caption
legend<-get_legend(violin_plot(e = c("Fe","Ca"))+
theme(legend.position = "right"))
plot_grid(legend)
ggsave("Figuere_B_Legend.png", device = png, width = 160, height = 207, units = "mm")#A4 margens and space for caption
library(tidyverse)
library(patchwork)
library(cowplot)
library(scales)
library(ggpmisc)
library(tidymodels)
library(RColorBrewer)
#my functons============
transpose_tibble<-function(tibble){
tibble%>%
rename(rowname=1)%>%
mutate(across(everything(),~as.character(.)))%>%
pivot_longer(cols = !rowname ,names_to = 'variable',  values_to = 'value') %>%
pivot_wider(id_cols = variable, names_from = rowname, values_from = value)%>%
type.convert(as.is=TRUE)
}
my_theme<-theme_light()+
theme(
strip.background = element_blank(),
panel.spacing.x = unit(.25, "lines"),
panel.spacing.y = unit(0, "lines"),
panel.border = element_rect(color= "black"),
#legend.position = "none",
plot.margin = margin(t = c(0.0, 0.0, 0.0, 0.0) , unit = "cm"),
axis.ticks = element_line(color = "black"),
text = element_text(size = 9,
color = "black",
family = "Arial"),
plot.title = element_text(face = "bold", size = 10),
strip.text = element_text(hjust = 0,
face = "bold",
size = 10,
color = "black"),
)
setwd("C:/Users/s299038/OneDrive - Cranfield University/1. Rapid Measurement Tools - Niall/Chapter 3 - XRF Analysis Accuracy on Slag/Data Analysis/combining for analysis")
pivoted_data<-read_csv("filterd_data.csv")
setwd("C:/Users/s299038/OneDrive - Cranfield University/1. Rapid Measurement Tools - Niall/Chapter 3 - XRF Analysis Accuracy on Slag/Data Analysis/Fig_C RSD")
setwd("C:/Users/s299038/OneDrive - Cranfield University/1. Rapid Measurement Tools - Niall/Chapter 3 - XRF Analysis Accuracy on Slag/Data Analysis/combining for analysis")
element_orders<-read_csv("instrument_elements.csv")
elements_conc<-element_orders$Conc_Order_reverse%>%na.omit()
elements_atomic<-element_orders$Atomic_Order%>%na.omit()
elements_alphabetical<-element_orders$Alphabetic_Order%>%na.omit()
Prosesing_steps<-c("raw","sived","dried","vessile","ground","ignited","ICP")
setwd("C:/Users/s299038/OneDrive - Cranfield University/1. Rapid Measurement Tools - Niall/Chapter 3 - XRF Analysis Accuracy on Slag/Data Analysis/Fig_C RSD")
#change the leval ordering so the graph is in the write order
mutated_data<-pivoted_data%>%
filter(element%in%c("Fe","Ca","Ti","Zn","P","K","Mn","Cr","Ni","As","Pb","Sr"),
!is.na(RSD))%>%
mutate(Prosessing_Step = fct_relevel(Prosessing_Step, c(Prosesing_steps)),
element = fct_relevel(element,c(elements_conc)))
#faceted by prosesing step
mutated_data%>%
ggplot(aes(x= element,y= RSD,fill= element))+
geom_violin(width=1.4,trim = FALSE)+
geom_boxplot(width=0.2, color="black", alpha=.9,lwd = 0.1,outlier.size = 0.2,outlier.shape = "cross" )+
facet_wrap(vars(Prosessing_Step),ncol=4)+
scale_y_continuous(limits = c(0,100),
expand = c(0,0),
breaks = c(0,20,40,60,80,100),
minor_breaks = c(10,30,50,70,90))
#facceted by element
mutated_data%>%
ggplot(aes(x= Prosessing_Step,y= RSD,fill= Prosessing_Step))+
geom_violin(width = 1.4,trim = FALSE,lwd = 0.1)+
geom_boxplot(width = 0.2, color = "black", alpha = 0.9,lwd = 0.1,outlier.size = 0.2,outlier.shape = "cross" )+
facet_wrap(vars(element),ncol=3)+
scale_y_continuous(limits = c(0,100),
expand = c(0,0),
breaks = c(0,20,40,60,80,100),
minor_breaks = c(10,30,50,70,90))+
theme(legend.position = "none",
axis.text.x = element_text(angle=45,hjust = 1))
#facceted by element box only
mutated_data%>%
ggplot(aes(x= Prosessing_Step,y= RSD,fill= Prosessing_Step))+
geom_boxplot(width = 0.9, color = "black", alpha = 1,lwd = 0.1,outlier.size = 1,outlier.shape = "cross" )+
stat_summary(fun = mean, geom = "point", shape = "cross", size = 1.5, color = "black")+
facet_wrap(vars(element),ncol=3)+
scale_y_continuous(limits = c(0,100),
expand = c(0,0),
breaks = c(0,20,40,60,80,100),
minor_breaks = c(10,30,50,70,90))+
theme(legend.position = "none",
axis.text.x = element_text(angle=45,hjust = 1))
#facceted by element box only
mutated_data%>%
ggplot(aes(x= Prosessing_Step,y= RSD,fill= Prosessing_Step))+
geom_boxplot(width = 0.9, color = "black", alpha = 1,lwd = 0.1,outlier.size = 1,outlier.shape = "cross" )+
stat_summary(fun = mean, geom = "point", shape = "cross", size = 1.5, color = "black")+
facet_wrap(vars(element),ncol=3)+
scale_y_continuous(limits = c(0,100),
expand = c(0,0),
breaks = c(0,20,40,60,80,100),
minor_breaks = c(10,30,50,70,90))+
my_theme+
theme(legend.position = "none",
axis.text.x = element_text(angle=45,hjust = 1))
#facceted by element box only
mutated_data%>%
ggplot(aes(x= Prosessing_Step,y= RSD,fill= Prosessing_Step))+
geom_boxplot(width = 0.9, color = "black", alpha = 1,lwd = 0.1,outlier.size = 1,outlier.shape = "cross" )+
stat_summary(fun = mean, geom = "point", shape = "cross", size = 1.5, color = "black")+
facet_wrap(vars(element),ncol=3)+
scale_y_continuous(limits = c(0,100),
expand = c(0,0),
breaks = c(0,20,40,60,80,100),
minor_breaks = c(10,30,50,70,90))+
my_theme+
theme(legend.position = "none",
axis.text.x = element_text(angle=45,hjust = 1))+
xlab("Prosessing Step")+
ylab("Relative Standard Deviation (%)")
#facceted by element box only
mutated_data%>%
ggplot(aes(x= Prosessing_Step,y= RSD,fill= Prosessing_Step))+
geom_boxplot(width = 0.9, color = "black", alpha = 1,lwd = 0.1,outlier.size = 1,outlier.shape = "cross" )+
stat_summary(fun = mean, geom = "point", shape = "cross", size = 1.5, color = "black")+
facet_wrap(vars(element),ncol=3)+
scale_y_continuous(limits = c(0,100),
expand = c(0,0),
breaks = c(0,20,40,60,80,100),
minor_breaks = c(10,30,50,70,90))+
my_theme+
theme(legend.position = "none",
axis.text.x = element_text(angle=45,hjust = 1))+
xlab("Prosessing Step")+
ylab("Relative Standard Deviation (%)")
ggsave("Figuere_3_boxplot.png", width = 160, height = 150, units = "mm")
ggsave("Figuere_3_boxplot_lanscape.png", width = 250, height = 150, units = "mm")
